package org.jiamandan.com.strategy.listener;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.jiamandan.com.config.UdpServerHandler;
import org.jiamandan.com.constant.DXConstant;
import org.jiamandan.com.dto.DXBasicDto;
import org.jiamandan.com.entity.RechargePile;
import org.jiamandan.com.service.IRechargeGunService;
import org.jiamandan.com.service.IRechargePileService;
import org.jiamandan.com.strategy.IDXContextListener;
import org.jiamandan.com.utils.DXObjectUtils;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service(value = DXConstant.Service.CMD_902)
@AllArgsConstructor
@Slf4j
public class DX902DeviceHeartListenerService implements IDXContextListener {


    private IRechargePileService iRechargePileService;
    private IRechargeGunService iRechargeGunService;

    private static final int[] number = {2, 4, 8, 16, 24, 26, 30, 32};

    @Override
    public void doExecute(DXBasicDto dto, Channel ctx) throws Exception {
        log.info("接收嘉满电-DX获取902上传心跳包(CMD=902)【"+dto.getDeviceNo()+"】报文:" + JSON.toJSONString(dto));
        reply(dto); //及时回复避免设备断开连接
        // 发送到Rocketmq
        String smg = DXObjectUtils.Object2String(dto);

     //   rocketMQTemplate.asyncSend(mqProperties.getOrderChargeProfitTopic() + ":" + BizEventTag.DX_ACTUAL_DATA.name(), smg, RocketmqSendCallbackBuilder.commonCallback());


        //3AA3000000018680650738780960
        // 00AF
        // 0902
        // 00 每次刷卡流水加 1
        // 00 00 00 00      卡号
        // 00               范围 0-16，为 0 表示还未选择，上位机需要等待
        // 09               4G 的信号强度，范围 0-3
        // 00               保留
        // 0A               几个充电桩

        // 00       00空闲没有充电，01：正在充电，02：故障状态
        // 00       00：计时模式，01：计量模式
        // 00 00     单位为毫安，当前的充电电流
        // 00 00 00 00          当前充电已用的时间，单位为妙
        // 00 00 00 00          当前充电已用的电量，单位为瓦时，千分之一度
        // 00                   00没有停止，01充电时间到，02充电电量到 03电流超出限制，04充满自停，05拔出自停，06保险丝熔断，07软件停止充电，08计量芯片错
        // 00 00            单位为 W，当前的充电功率
        // 00               保留
        // 00000000000000000000000000000000
        // 00000000000000000000000000000000
        // 00000000000000000000000000000000
        // 00000000000000000000000000000000
        // 00000000000000000000000000000000
        // 00000000000000000000000000000000
        // 00000000000000000000000000000000
        // 00000000000000000000000000000000
        // 00000000000000000000000000000000
        // 00 00 00 00
        // 15C8

    }

    static  void reply(DXBasicDto dto) {
        Date now = new Date();
        String year = DateFormatUtils.format(now,"yyyy");
        String month = DateFormatUtils.format(now,"MM");
        String days = DateFormatUtils.format(now,"dd");
        String hour = DateFormatUtils.format(now,"HH");
        String minutes = DateFormatUtils.format(now,"mm");
        String second =  DateFormatUtils.format(now,"ss");

        String[] weekStr={"06","00","01","02","03","04","05"};
        StringBuilder response = new StringBuilder();
                    response.append(StringUtils.leftPad(Integer.toHexString(Integer.valueOf(year)),4,"0"))
                            .append(StringUtils.leftPad(Integer.toHexString(Integer.valueOf(month)),2,"0"))
                            .append(StringUtils.leftPad(Integer.toHexString(Integer.valueOf(days)),2,"0"))
                            .append(StringUtils.leftPad(Integer.toHexString(Integer.valueOf(hour)),2,"0"))
                            .append(StringUtils.leftPad(Integer.toHexString(Integer.valueOf(minutes)),2,"0"))
                            .append(StringUtils.leftPad(Integer.toHexString(Integer.valueOf(second)),2,"0"))
                            .append(weekStr[Integer.valueOf(StringUtils.leftPad(String.valueOf(DateUtil.thisDayOfWeek() - 1), 2, '0'))]);
        try {
            RechargePile rechargePile = new RechargePile();
            rechargePile.setPileNo(dto.getDeviceNo());
            UdpServerHandler.sendMessage(response.toString(),dto,"00",DXConstant.Cmd.CMD_902);
        }catch (Exception e){
            log.error("发送嘉满电-DX实时主动数据上传心跳包(CMD=902)【"+dto.getDeviceNo()+"】报文失败："+e.getMessage());
        }
    }

}
